<form id="register-form" action="<?= site_url("login/register")?>" method="post">
<table align="center">
	<tr>
		<td width="500px">
			<div id="registration-panel" class="easyui-panel" title="Registration" style="padding:10px;">
				<input type="hidden" value="new" name="new-or-not" id="new-or-not"/>
				<table>
					<tr>
						<td width="150px">First name:</td>
						<td><input class="easyui-validatebox" size="40" name="firstname" type="text" value="<?= $firstData["firstname"]?>" required="true"/></td>
					</tr>
					<tr>
						<td>Last name:</td>
						<td><input class="easyui-validatebox" size="40" name="lastname" type="text" value="<?= $firstData["lastname"]?>" required="true"/></td>
					</tr>
					<tr>
						<td>Email:</td>
						<td><input readonly="true" size="40" name="email" type="text" value="<?= $firstData["email"]?>"/></td>
					</tr>
					<tr>
						<td>Role:</td>
						<td>
							<input name="role" type="radio" value="association" checked="true">Association
							<input name="role" type="radio" value="user">User
						</td>
					</tr>
					<tr  id="association-line">
						<td>Associations:</td>
						<td>
							<select id="associationslist" name="associationslist" style="width: 180px;"></select>
							<a href="#" class="easyui-linkbutton" id="not-in-list" iconCls="icon-add">In list</a>
						</td>
					</tr>
					<tr  id="role-line">
						<td>Role in association:</td>
						<td><input name="associationrole" type="radio" value="member" checked="true">Member
							<input name="associationrole" type="radio" value="admin">Administrator
						</td>
					</tr>
				</table>

			</div>

		</td>
	</tr>
	<tr id="new-association">
		<td width="300px">
			<div id="association-panel" class="easyui-panel" title="New Association" style="padding:10px;">
				<table>
					<tr>
						<td width="150px">Association name:</td>
						<td><input class="easyui-validatebox" size="40" id="association-name" name="association-name" type="text" required="true"/></td>
					</tr>
					<tr>
						<td>Email:</td>
						<td><input class="easyui-validatebox" size="40" id="association-email" name="association-email" type="text" required="true"  validType="email"/></td>
					</tr>
					<tr>
						<td>Address:</td>
						<td><input class="easyui-validatebox" size="40" id="association-address" name="association-address" type="text" required="true"/></td>
					</tr>
				</table>
			</div>
		</td>
	</tr>
	<tr>
		<td align="right"><a href="#" class="easyui-linkbutton" iconCls="icon-ok" id="register-btn" >Register</a></td>
	</tr>
</table>

</form>

<script type="text/javascript">

	$(function(){

		$("input[name='role']").change(function(){
			var role = $(this).val();
			
			if(role == 'association'){
				$('#role-line').show('slow');
				$("#association-line").show('slow');
				
				var linkText = $("#not-in-list").text();
				if(linkText == 'In list'){
					$('#new-association').show('slow');
					$('#association-name').validatebox({required:true}); 
					$('#association-address').validatebox({required:true}); 
					$('#association-email').validatebox({required:true});
				}else{
					$('#new-association').hide('slow');
					$('#association-name').validatebox({required:false}); 
					$('#association-address').validatebox({required:false}); 
					$('#association-email').validatebox({required:false});
				}
				
			}else{
				$('#role-line').hide('slow');
				$("#association-line").hide('slow');
				$('#new-association').hide('slow');
				$('#association-name').validatebox({required:false}); 
				$('#association-address').validatebox({required:false}); 
				$('#association-email').validatebox({required:false});
			}
		});
		$('#register-btn').click(function() {
			if($("#register-form").form('validate')) $('#register-form').submit();
		});
		
		$('#associationslist').combogrid({
			panelWidth:450,
			value:'1',

			idField:'id',
			textField:'raison_social',
			url:'<?= site_url("associations/get_all")?>',
			columns:[[
				{field:'raison_social',title:'Raison social',width:60},
				{field:'adresse',title:'Address',width:100},
				{field:'telephone',title:'Telephone',width:120},
				{field:'mail',title:'Mail',width:100}
			]]
		});

		
		$("#not-in-list").click(function() {
			var linkText = $(this).text();
			if(linkText == 'Not in list'){
				$('#new-association').show('slow');
				$(this).linkbutton({text:'In list'});
				$("#new-or-not").val('new');
				$('#association-name').validatebox({required:true}); 
				$('#association-address').validatebox({required:true}); 
				$('#association-email').validatebox({required:true}); 
				
			}else{
				$('#new-association').hide('slow');
				$(this).linkbutton({text:'Not in list'});
				$("#new-or-not").val('notnew');
				$('#association-name').validatebox({required:false}); 
				$('#association-address').validatebox({required:false}); 
				$('#association-email').validatebox({required:false}); 
			}
		});
		
	});
</script>